package com.punai.prodatalibrary.controller;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.dtflys.forest.annotation.Get;
import com.dtflys.forest.annotation.GetRequest;
import com.punai.common.annotation.Anonymous;
import com.punai.common.core.domain.RestResponse;
import com.punai.prodatalibrary.bo.AboutUserBo;
import com.punai.prodatalibrary.domain.LibProject;
import com.punai.prodatalibrary.domain.LibProjectInfo;
import com.punai.prodatalibrary.domain.vo.SearchProAndInfoVo;
import com.punai.prodatalibrary.service.ILibProjectInfoService;
import com.punai.prodatalibrary.service.ILibProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/prolib/api")

public class ProLibApiController {

    @Autowired
    private ILibProjectService projectService;
    @Autowired
    private ILibProjectInfoService projectInfoService;
    @Anonymous
    @PostMapping("/getProjectInfo")
    public RestResponse<LibProjectInfo> getProjectInfo(@RequestBody SearchProAndInfoVo vo) {
        try{
            LibProjectInfo one = projectInfoService.getOne(
                    Wrappers.<LibProjectInfo>lambdaQuery()
                            .eq(StrUtil.isNotBlank(vo.getId()), LibProjectInfo::getId, vo.getId())
                            .eq(StrUtil.isNotBlank(vo.getCode()), LibProjectInfo::getProjectCode, vo.getCode())
                            .eq(StrUtil.isNotBlank(vo.getProId()), LibProjectInfo::getProId, vo.getProId())
            );
            return RestResponse.ok(one);
        }catch (Exception e) {
            return RestResponse.fail("获取失败！",e.getMessage());
        }
    }
    @Anonymous
    @PostMapping("/getProject")
    public RestResponse<LibProject> getProject(@RequestBody SearchProAndInfoVo vo) {
        try{
            LibProject one = projectService.getOne(
                    Wrappers.<LibProject>lambdaQuery()
                            .eq(StrUtil.isNotBlank(vo.getId()), LibProject::getId, vo.getId())
                            .eq(StrUtil.isNotBlank(vo.getCode()), LibProject::getCode, vo.getCode())
                            .eq(StrUtil.isNotBlank(vo.getPmproinfoid()), LibProject::getPmproinfoid, vo.getPmproinfoid())
            );
            return RestResponse.ok(one);
        } catch (Exception e) {
            return RestResponse.fail("获取失败！",e.getMessage());
        }
    }
    @Anonymous
    @PostMapping("/getProjectP2X")
    public RestResponse<LibProject> getProjectP2X(@RequestBody SearchProAndInfoVo vo) {
        try{
            LibProject one = projectService.getOne(
                    Wrappers.<LibProject>lambdaQuery()
                            .eq(StrUtil.isNotBlank(vo.getId()), LibProject::getId, vo.getId())
                            .eq(StrUtil.isNotBlank(vo.getCode()), LibProject::getCode, vo.getCode())
                            .eq(StrUtil.isNotBlank(vo.getPmproinfoid()), LibProject::getPmproinfoid, vo.getPmproinfoid())
            );
            return RestResponse.ok(one);
        } catch (Exception e) {
            return RestResponse.fail("获取失败！",e.getMessage());
        }
    }

    private List<String> getUsernames(String jsonStr) {
        List<AboutUserBo> aboutUserBos = JSON.parseArray(jsonStr, AboutUserBo.class);
        List<String> nos = new ArrayList<>();
        if (CollUtil.isNotEmpty(aboutUserBos)) {
            for (AboutUserBo aboutUserBo : aboutUserBos) {
                String cno = aboutUserBo.getCno();
                nos.add(cno);
            }
        }
        return nos;
    }

    @PostMapping("/saveOrUpdateProject")
    public RestResponse<LibProject> saveProject(@RequestBody LibProject vo) {
        try{
            if (vo != null) {
                if (StrUtil.isNotBlank(vo.getId())) {
                    String id = vo.getId();
                    // 需要处理 about_user_list
                    // 如果已经配置了用户 则判定是否存在 不存在新增
                    LibProject one = projectService.getById(id);
                    // 原项目人员
                    String dbAboutUser = one.getAboutUser();
                    List<String> dbUsernames = getUsernames(dbAboutUser);
                    String aboutUserList = one.getAboutUserList();
                    List<String> list = JSON.parseArray(aboutUserList, String.class);
                    // 防止不可变list
                    list = new ArrayList<>(list);
                    // 新项目人员
                    String aboutUser = vo.getAboutUser();
                    List<String> usernames = getUsernames(aboutUser);
                    // 原项目人员 - 新项目人员 = 应该被排除的人员
                    dbUsernames.removeAll(usernames);
                    if (CollUtil.isNotEmpty(dbUsernames)) {
                        // 排除
                        list.removeAll(dbUsernames);
                    }
                    // 去重复
                    usernames.removeAll(list);
                    if (CollUtil.isNotEmpty(usernames)) {
                        list.addAll(usernames);
                    }
                    String aboutUserListJson = JSON.toJSONString(list);
                    vo.setAboutUserList(aboutUserListJson);
                    projectService.updateById(vo);
                } else {
                    // 需要处理 about_user_list
                    String aboutUser = vo.getAboutUser();
                    List<String> nos = getUsernames(aboutUser);
                    String aboutUserListJson = JSON.toJSONString(nos);
                    vo.setAboutUserList(aboutUserListJson);
                    projectService.save(vo);
                }
                return RestResponse.ok(vo);
            }
        }catch (Exception e){
            return RestResponse.fail("保存失败！",e.getMessage());
        }
        return RestResponse.fail("保存失败！","空对象");
    }

    @PostMapping("/saveOrUpdateProjectInfo")
    public RestResponse<LibProjectInfo> saveProjectInfo(@RequestBody LibProjectInfo vo) {
        try{
            if (vo != null) {
                boolean result;
                if (StrUtil.isNotBlank(vo.getId())) {
                    Date projectStartDate = vo.getProjectStartDate();
                    Date projectEndDate = vo.getProjectEndDate();
                    projectInfoService.lambdaUpdate().eq(LibProjectInfo::getId, vo.getId())
                            .set(projectEndDate == null,LibProjectInfo::getProjectStartDate, null)
                            .set(projectStartDate == null,LibProjectInfo::getProjectEndDate, null);
                    projectInfoService.updateById(vo);
                } else {
                    projectInfoService.save(vo);
                }
                return RestResponse.ok(vo);
            }
        }catch (Exception e){
            return RestResponse.fail("保存失败！",e.getMessage());
        }
        return RestResponse.fail("保存失败！","空对象");
    }

}
